%% Our 290 function minimum finder

clc
close all
clear MF
clear classes

%Setup a function with an 'unknown' minimum
a=rand();
f = @(x) (x-a).^2;

%Setup our object
MF=MinFinder290();
MF.A=0;
MF.B=1;
MF.X0=.5;
MF.F=f;
MF.StepSize=.25;
MF.TolX=1e-8;

%Run the finder 
tic
MF.finder290();
toc

%Plot the results
MF.plot290()

fprintf('Actual: %g, Found: %g\n',a,MF.X(end))

%We can use the profiler to find what parts of code run the slowest:
profile on
MF.finder290();
profile viewer


%% Try the golden search method

%update once on interval
A=0;
B=1;
r=(-1+sqrt(5))/2
X0=(B-A)*r+A
a=rand();
f = @(x) (x-a).^2;

X=[A X0 B];
FX=f(X);

% Make some plots showing interval evolution
close all

Xvec=linspace(A,B,100);
Fvec=f(Xvec);

figure
plot(Xvec,Fvec)
xlabel('X')
ylabel('F(X)')
Ymin=min(Fvec);
Ymax=max(Fvec);
hold on

%plot updated intervals
for ii=1:10
    %gray out old interval plots
    if ii>1
        for jj=1:3
            PobjVec(jj).Color=[.75,.75,.75];
        end
    end
    
    %Plot the new intervals
    for jj=1:3
        PobjVec(jj)=plot([X(jj) X(jj)],[Ymin Ymax],'k--');
    end
    
    %Update the intervals
    [X,FX]=updateGRInterval(X,FX,f);
    
    %pause so we can see update
    pause(1)
end

% Now use the above in a class and compare to 290 finder.....













